﻿using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Models;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Maps;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.Facility
{
    /// <summary>
    /// 选择工厂模型视图模型
    /// </summary>
    public class SelectFacilityViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 基础定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IFoundationContract> _foundationContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public SelectFacilityViewModel(ServiceProxy<IFoundationContract> foundationContract)
        {
            this._foundationContract = foundationContract;
        }

        #endregion

        #region 属性

        #region 工厂模型列表 —— ObservableCollection<Node> Facilities
        /// <summary>
        /// 工厂模型列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Node> Facilities { get; set; }
        #endregion

        #region 已选工厂模型 —— Node SelectedFacility
        /// <summary>
        /// 已选工厂模型
        /// </summary>
        [DependencyProperty]
        public Node SelectedFacility { get; set; }
        #endregion

        #endregion

        #region # 方法

        //Initializations

        #region 初始化 —— override async Task OnInitializeAsync(CancellationToken cancellationToken)
        /// <summary>
        /// 初始化
        /// </summary>
        protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
        {
            IEnumerable<FacilityInfo> facilityInfos = await Task.Run(() => this._foundationContract.Channel.GetFacilities(null, null, null, null), cancellationToken);
            ICollection<Node> facilityTree = facilityInfos.ToTree(null);
            this.Facilities = new ObservableCollection<Node>(facilityTree);
        }
        #endregion


        //Actions

        #region 选择工厂模型 —— void SelectFacility(Node node)
        /// <summary>
        /// 选择工厂模型
        /// </summary>
        /// <param name="node">节点</param>
        public void SelectFacility(Node node)
        {
            this.SelectedFacility = node;
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            if (this.SelectedFacility == null)
            {
                MessageBox.Show("请选择工厂模型！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion

            await base.TryCloseAsync(true);
        }
        #endregion

        #endregion
    }
}
